import {
  stateType, partStateType,
  dispatchType, stateOfDispatchType,
  userDataType,
  songsInfoPlayType
} from '@/interfaces/types'
export const initState: stateType = {
  songsInfoPlay: [], //{songId:{href}}*******用Object.keys(obj)、values获取  key转换成 number
  songsInfoHis: [],
  songsLike: {changeTag:false,value:[]},
  playInfo: { playingId: -1, isPlay: false, playingIndex: -1, currentTime: -1 },//playingId 只读，通过更改playingIndex来更改playingId
  showList: false,
  sIPC: true,  //songsInfoPlay变化标记，便于观测,节省观测成本
  sIHC: true,
  userData: null
};
const stateOfDispatch: stateOfDispatchType = {
  SET_STATE://songsInfoPlay,songsInfoHis,playInfo    改,增前,改,保存  仅供functions使用
    (state: stateType, value: partStateType) => {
      return {//value:{infoplay,infohis,showsong}
        ...state,
        ...value
      }
    },
  DELETE_ALL://songsInfoPlay,songsInfoHis,playInfo    复原,保存,复原,保存
    (state: stateType) => {
      return {
        ...initState,
        songsInfoHis: state.songsInfoHis,
        showList: state.showList,
        sIPC: !state.sIPC
      }
    },
  DELETE_HIS://songsInfoPlay,songsInfoHis,playInfo    保存，复原，保存,保存
    (state: stateType) => {
      return {
        ...state,
        songsInfoHis: initState.songsInfoHis,
        sIHC: !state.sIHC
      }
    },
  //{playInfo:{Id,is}}
  //{showList:XXX}
  SET_SHOW:
    (state: stateType) => {
      return {

        ...state,
        showList: !state.showList
      }
    },
  TOPLAY:
    (state: stateType) => {
      return {
        ...state,
        playInfo: { ...state.playInfo, isPlay: true }
      }
    },
  TOPAUSE:
    (state: stateType) => {
      return {
        ...state,
        playInfo: { ...state.playInfo, isPlay: false }
      }
    },
  SET_CURRENTTIME:
    (state: stateType, value: { currentTime: number }) => {
      return {
        ...state,
        playInfo: { ...state.playInfo, ...value }
      }
    },
  SET_USERDATA:
    (state: stateType, value: { userData: userDataType | null }) => {
      return {
        ...state,
        userData: value.userData
      }
    }

}
const reducer = (state: stateType = initState, action: dispatchType) => {
  if (stateOfDispatch[`${action?.type}`]) {
    return stateOfDispatch[`${action.type}`](state, action.value)
  } else {
    return {
      ...state
    }
  }
}
const useStore = () => ({ store: initState, reducer });
export default useStore;
